package org.karson.designpattern.behavioral.chainofresponsibility.core.linkedlist;

public class HandlerChain {
    private Handler head = null;
    private Handler tail = null;

    public void addHandler(Handler handler) {
        handler.setSuccessor(null);

        // 1. 首次head和tail指针都指向第一个元素
        if (head == null) {
            head = handler;
            tail = handler;
            return;
        }

        // 2. 设置tail的successor为参数
        tail.setSuccessor(handler);
        // 3. tail指向参数
        tail = handler;
    }

    public void handle() {
        if (head != null) {
            head.handle();
        }
    }
}